/**
 * 项目版本添加接口测试用例
 *
 * Created by nongzhihzhong on 2021/11/22.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));
const common = require('../../../common');
const Administrator = require('../../../../models/administrator');
const uri = require('../../uri');
const benchmark = require('./../../../../benchmark/integration-test/versionInfo');
module.exports = describe('/GET project-versions/:versionId/version-config.json', function () {
  const administrator = new Administrator();
  let project = {};
  let projectVersion = {};
  const newUserIds = [];
  let newUser;
  // let analyzeTemplateId;
  const expectedData = benchmark.result;
  function url(versionId) {
    if (!versionId) {
      versionId = projectVersion.versionId;
    }
    return `/project-versions/${versionId}/version-config.json`;
  }
  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        newUser = tester;
        return newUser.createRandomProject();
      })
      // .then((tester) => {
      //   newUserIds.push(tester.userId);
      //   newUser = tester;
      //   return tester.createTemplate([]);
      // })
      // .then((template) => {
      //   analyzeTemplateId = template._id.toString();
      //   benchmark.config.staticAnalyze.analyzeTemplate = analyzeTemplateId;
      //   expectedData.staticAnalyze.analyzeTemplate = analyzeTemplateId;
      //   return newUser.createRandomProject();
      // })
      .then((res) => {
        project = res;
        return project.createRandomVersion();
      })
      .then((res) => {
        projectVersion = res;
        return projectVersion.updateVersionFiles(benchmark.files);
      })
      .then(() => project.updateVersion(projectVersion.versionId, benchmark.config))
      .then(() => project.getVersion(projectVersion.versionId))
      .then((res) => {
        expectedData.repository = res.repository;
        expectedData.rootDirectory = res.rootDirectory;
        expectedData.versionId = projectVersion.versionId;
        expectedData.versionName = projectVersion.versionName;
      })
      .then(() => {
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'get');
  common.should.be.tester(uri, url, 'get');
  it('it should GET a project version config', function (done) {
    project.creator.login()
      .then(() => {
        chai.request(uri)
          .get(url())
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(200);
            res.body.staticAnalyze.analyzeTemplate = '';
            common.should.be.equal.versionConfig(res.body, expectedData);
            done();
          });
      }).catch(done);
  });
});
